﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using log4net;
using log4net.Config;
using DTO;
using AutoMapper;

namespace DataAccess
{
    public static class AccountDAO
    {
        private static readonly ILog logger = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
        static AccountDAO()
        {
            XmlConfigurator.Configure();         
        }

        public static List<AccountDTO> GetAccountList()
        {

            try
            {
                List<AccountDTO> lstAccount = new List<AccountDTO>();

                QUANLYBONGDA_VDQG_1042102Entities context = new QUANLYBONGDA_VDQG_1042102Entities();
                var query = from Account in context.ACCOUNT_LIST
                            select Account;
                foreach (var item in query)
                {
                    AccountDTO dtoAccount = new AccountDTO();
                    dtoAccount = Mapper.Map<ACCOUNT, AccountDTO>(item);
                    lstAccount.Add(dtoAccount);
                }
                return lstAccount;
            }
            catch (Exception ex)
            {
                logger.Error(ex);
                return null;
            }
        }

        public static AccountDTO FindAccountByUserName(AccountDTO dtoAccount)
        {
            try
            {                
                QUANLYBONGDA_VDQG_1042102Entities context = new QUANLYBONGDA_VDQG_1042102Entities();
                var query = from Account in context.ACCOUNT_LIST
                            where Account.USERNAME ==dtoAccount .USERNAME 
                            select Account;
                if (query.Count() == 0)
                    return null;
                AccountDTO Result = new AccountDTO();
                ACCOUNT acc = query.First();
                acc.ACCOUNT_TYPEReference.Load();
                Result=Mapper.Map<ACCOUNT, AccountDTO>(acc);
                return Result;
            }
            catch (Exception ex)
            {                
                logger.Error(ex);
                return null;
            }
        }

        public static bool AddNewAccount(AccountDTO dtoAccount)
        {
            try
            {
                ACCOUNT  obj = new ACCOUNT ();
                //Mapping                
                obj = Mapper.Map<AccountDTO, ACCOUNT >(dtoAccount);
                QUANLYBONGDA_VDQG_1042102Entities context = new QUANLYBONGDA_VDQG_1042102Entities();
                //Kiểm tra Account Type đã tồn tại chưa
                obj.ACCOUNT_TYPE = context.ACCOUNT_TYPE_LIST.First(at => at.ACCOUNT_TYPE_ID == dtoAccount.ACCOUNT_TYPE.ACCOUNT_TYPE_ID);
                context.AddToACCOUNT_LIST(obj);
                context.SaveChanges(true);                
                return true;
            }
            catch (Exception ex)
            {
                logger.Error(ex);
                return false;
            }
        }

        public static bool UpdateAccount(AccountDTO dtoAccount)
        {
            try
            {
                QUANLYBONGDA_VDQG_1042102Entities context = new QUANLYBONGDA_VDQG_1042102Entities();

                ACCOUNT  Account = context.ACCOUNT_LIST.First(g => g.USERNAME  == dtoAccount.USERNAME );
                Account.PASSWORD  = dtoAccount.PASSWORD ;
                //Xem Account Type có gì mới không
                if (Account.ACCOUNT_TYPE.ACCOUNT_TYPE_ID != dtoAccount.ACCOUNT_TYPE.ACCOUNT_TYPE_ID)
                {
                    Account.ACCOUNT_TYPE = context.ACCOUNT_TYPE_LIST.First(at => at.ACCOUNT_TYPE_ID == dtoAccount.ACCOUNT_TYPE.ACCOUNT_TYPE_ID);
                }
                context.SaveChanges(true);
                return true;
            }
            catch (Exception ex)
            {
                logger.Error(ex);
                return false;
            }
        }

        public static bool DeleteAAccount(AccountDTO dtoAccount)
        {
            try
            {
                QUANLYBONGDA_VDQG_1042102Entities context = new QUANLYBONGDA_VDQG_1042102Entities();
                ACCOUNT  Account= context.ACCOUNT_LIST .First(g => g.USERNAME  == dtoAccount .USERNAME );
                context.DeleteObject(Account);
                context.SaveChanges();
                return true;
            }
            catch (Exception ex)
            {
                logger.Error(ex);
                return false;
            }
        }

        public static bool DeleteAllAccount()
        {
            try
            {
                QUANLYBONGDA_VDQG_1042102Entities context = new QUANLYBONGDA_VDQG_1042102Entities();
                var query = (from account in context.ACCOUNT_LIST 
                             select account);
                foreach (var item in query)
                {
                    context.DeleteObject(item);
                }
                context.SaveChanges();
                return true;
            }
            catch (Exception ex)
            {
                logger.Error(ex);
                return false;
            }
        }
        
    }
}
